package com.ostrich.cloud.ostrichGoodsService.app.ApiImp;

import cn.hutool.core.bean.BeanUtil;
import com.ostrich.cloud.common.exceptions.OstrichException;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichGoodsSercice.model.Dto.SecKillDto;
import com.ostrich.cloud.ostrichGoodsSercice.model.param.AddSecKillParam;
import com.ostrich.cloud.ostrichGoodsSercice.model.param.SnappedGoodParam;
import com.ostrich.cloud.ostrichGoodsService.app.component.Utlis;
import com.ostrich.cloud.ostrichGoodsService.app.createJob.XxlJobCreateParam;
import com.ostrich.cloud.ostrichGoodsService.app.createJob.XxlJobManager;
import com.ostrich.cloud.ostrichGoodsService.client.SecKillClient;
import com.ostrich.cloud.ostrichGoodsService.common.exception.OstrichGoodsException;
import com.ostrich.cloud.ostrichGoodsService.common.exception.OstrichGoodsExceptionCode;
import com.ostrich.cloud.ostrichGoodsService.infra.model.SecKill;
import com.ostrich.cloud.ostrichGoodsService.service.SecKillService;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
public class SecKillApiImp implements SecKillClient {

    @Resource
    private SecKillService secKillService;

    @Resource
    private XxlJobManager xxlJobCreator;


    @Override
    public Result addSecKill(AddSecKillParam addSecKillParam) throws Exception {

        SecKill secKill = BeanUtil.copyProperties(addSecKillParam, SecKill.class);
        SecKill addSecKill = secKillService.addSecKill(secKill);
        XxlJobCreateParam param = new XxlJobCreateParam();
        param.setExecutorId(2);
        param.setJobDesc("秒杀到点取消任务 ");
        param.setScheduleConf(Utlis.dateToCron(secKill.getEndTime()));
        param.setExecutorHandler("closeSecKill");
        param.setExecutorParam(addSecKill.getSecId()+"");
        int jobId = xxlJobCreator.createJob(param);
        return Result.okm("秒杀获得添加成功");
    }

    @Override
    public Result getSecKill(String status) {
        List<SecKillDto> secKill = secKillService.getSecKill(status);
        return Result.ok(secKill);
    }


    public Result snappedGood(Integer secId) {
        return null;
    }


    public Result snappedGood(SnappedGoodParam snappedGoodParam) {
        Integer parentId = Utlis.getParentId();
        if (parentId==null){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.NOT_USER_EXCEPTION);
        }
        secKillService.snappedGood(snappedGoodParam,parentId);
        return Result.okm("抢购成功");
    }

    @Override
    public Result getSecKillResult(Integer secId) {
        Integer parentId = Utlis.getParentId();
        SecKillDto secKillResult = secKillService.getSecKillResult(parentId, secId);
        return Result.ok(secKillResult);
    }

    @Override
    public Result cancelSecKill(Integer parentId, Integer orderId) throws Exception {
        secKillService.cancelSecKill(parentId,orderId);
        return Result.ok();
    }

    private String dateToCron(LocalDateTime date) {
        String cronFormat="ss mm HH dd MM ? yyyy";
        return DateTimeFormatter.ofPattern(cronFormat).format(date);
    }
}
